package com.majsubtense.core;

import com.majsubtense.core.events.Event;
import com.majsubtense.core.events.EventHandler;
import com.majsubtense.core.events.EventDispatcher;

import java.util.ArrayList;
import java.util.List;

/**
 * @brief 此类向下派生作为事件发送者，事件监听者使用 addListener 注册事件处理函数。
 *        向下派生后可以一监听多，也可以多监听一。
 */
public abstract class Notifier {
    protected static List<EventHandler<? extends Event>> eventHandlers = new ArrayList<>();

    public static void addListener(EventHandler<? extends Event> eventHandler) {
        eventHandlers.add(eventHandler);
    }

    public static void removeListener(EventHandler<? extends Event> eventHandler) {
        eventHandlers.remove(eventHandler);
    }

    public static void onDeliver(Event event) {
        EventDispatcher dispatcher = new EventDispatcher(event);
        for (EventHandler<? extends Event> eventHandler : eventHandlers)
            dispatcher.dispatch(eventHandler);
    }
}
